package.cpath = "./skynet/luaclib/?.so;".. package.cpath
package.path = "./skynet/lualib/?.lua;./app/common/lib/?.lua;./testclient/?.lua;" .. package.path

if _VERSION ~= "Lua 5.3" then
	error "Use lua 5.3"
end

local socket = require "client.websocket"
local protobuf = require "protobuf"
local parser = require "parser"

local md5 = require "md5"

parser.register("./app/proto/primary.proto")
parser.register("./app/brnnnode/brnn.proto")

local platform2login_key = "p2l_salt"

local decode = require "protopack"

--local fd = assert(socket.connect("127.0.0.1", 8191))
local fd = assert(socket.connect("127.0.0.1", 8191))
local is_logined = false

local function send_package(fd, pack)
	-- 使用了websocket ，会自己组织帧格式的
	socket.send(fd, pack)
end

local function recv_package()
	local r , istimeout= socket.recv(fd, 100)
	if not r then
		return nil
	end
	if r == ""  and (not istimeout ) then
		error "Server closed"
	end
	return r
end


local function send_request(msgname, msg)
    if msgname ~= "heart" then
        print(msgname)
	    print(msg)
    end
	
	local str = decode.c2spack(protobuf, msgname, msg)
	send_package(fd, str)
end

local last = ""

local function print_request(name, args)
	print("REQUEST", name)
	if args then
		for k,v in pairs(args) do
			print(k,v)
		end
	end
end

local bet_btn_list = {1101,1102,1103,1104}
local function random_bet()
    local score = 100
    local cnt = math.random(1,6)

    for i = 1,cnt do
        for k,id in pairs(bet_btn_list) do
            local r = math.random(0,1)
            if r == 1 then
                send_request("brnn.doaction",{action_type = 1, bet_item_id = id, bet_score = score})
            end
        end
    end
    
end

local function print_response(msghead, msgbody)
    if msghead.msgname == "heartR" then
        return
    end

    print('------ begin response --------')
	if msghead then
		for k,v in pairs(msghead) do
			print(k,v)
            if v == "authR" and msgbody then
                gate_ip = "127.0.0.1"
                rid = msgbody.rid  
                gateport = msgbody.gateport     
                expiretime = msgbody.expiretime
                gatetoken = msgbody.gatetoken
                username = msgbody.username
                cpass = msgbody.cpass
                fd = assert(socket.connect(gate_ip, gateport))
                if fd then
                    send_request("entergame", {rid=rid, uid=uid, gatetoken=gatetoken,expiretime = expiretime,agencyrid = agencyrid, clientver = "0000"})
                end
            elseif v == "brnn.todoN" then
                random_bet()
            end
		end
	end
	if msgbody then
		for k,v in pairs(msgbody) do
			print(k,v)
			if type(v) == "table" then
				print("-- table: ", k )
				for i,j in pairs(v) do
					print("  --", i,j)
                    if type(j) == "table" then
                        print("------ subtable: ", i)
                        for kk,vv in pairs(j) do
                            print("     ----",kk,vv)
                            if type(vv) == "table" then
                                print("     ------ subtable: ", kk)
                                for key,val in pairs(vv) do
                                    print("         ----",key,val)
                                end
                            end
                        end
                        
                    end
				end
			end
		end
	end
	print('------ end response --------')
end

local function dispatch_package()
	while true do
		local v
		v = recv_package()
		if not v  or v == "" then
			break
		end
		print_response(decode.s2cunpack(protobuf, v))
	end
end

local function gentoken(username)
    local tokentime = os.time()
    local newtoken = md5.sumhexa(username .. tokentime .. platform2login_key ) .. tokentime
    return newtoken
end

math.randomseed(os.time())
math.random()

local count = 1

while true do
	dispatch_package()
	local cmd = socket.readstdin()
	if cmd then
		if cmd == "awx" then
            local newtoken = gentoken("wxid_1311")
			send_request("authwx", {username = "wxid_1311", refresh_token = "123456", rolename = "abc", logintoken = newtoken})
        elseif cmd == "auo" then
			local newtoken = gentoken("wx_1111")
			send_request("authother", {username = "wx_1111", cpass = "123456", logintoken = newtoken})
		elseif cmd == "aur" then
			local newtoken = gentoken("wx_1111")
			send_request("authuser", {username = "wx_1111", cpass = "123456", logintoken = newtoken})
		elseif cmd == "agt" then
            local username = "46634e1c0013cdea0172" .. math.random(100000,999999)
			local newtoken = gentoken(username)
			send_request("authguest", {username = username, logintoken = newtoken})
        elseif cmd == "et" then
            send_request("brnn.entertable", {level_type = 1})
        elseif cmd == "st" then
            send_request("brnn.sitdowntable", {})
        elseif cmd == "rd" then
            send_request("brnn.gameready", {})
		elseif cmd == "lv" then
            send_request("brnn.leavetable", {})
        elseif tonumber(cmd) then
            local num = tonumber(cmd)
            local id = math.floor(num / 10000)
            local score = num % 10000
            send_request("brnn.doaction", {action_type = 1, bet_item_id = id, bet_score = score})
        elseif cmd == "cs" then
            send_request("brnn.doaction", {action_type = 2})
        elseif cmd == "rb" then
            send_request("brnn.doaction", {action_type = 3})
        elseif cmd == "sz" then
            send_request("brnn.doaction", {action_type = 4})
        elseif cmd == "xz" then
            send_request("brnn.doaction", {action_type = 5})
        elseif cmd == "heart" then
            send_request("brnn.nodeheart", {nodeallname = "brnn"})
        elseif cmd == "gl" then
            send_request("brnn.getlevellist", {nodename = "brnn"})
        end
	else
		socket.usleep(100)
        if is_logined then
            if count == 100 then
                count = 0
            else
                count = count + 1
            end
        end
	end
end